package com.example.state;

import android.content.Context;
import android.util.Log;
import android.view.MotionEvent;

import com.example.bounce.Bounce;
import com.example.bounce.R;
import com.example.glsurfaceview.MyGLSurfaceView;
import com.example.glutil.WorldCoord;
import com.example.object.screen.Background;
import com.example.object.screen.SignShape;
import com.example.object.screen.StarShape;

import org.jbox2d.common.Vec2;

/**
 * Created by Lucas on 06/06/13.
 */
public class Home extends AppState{

    /**
     * The debug TAG
     */
    private final String TAG = "HomeState";

    /**
     * Constructor of the Home state
     * @param context the context of the application
     * @param glView the GLSurfaceView
     */
    public Home(Context context, MyGLSurfaceView glView) {
        super(context, glView);
        _bounce = (Bounce)context;
    }

    /**
     * Initialize the list of game shape
     */
    @Override
    public void initGameShape(){
        Log.i(TAG, "Initialize game's shapes");
        _listGameShape.add(new Background(_context, AppState.SIMULATION_HEIGHT/2f, R.raw.backgroundhome));
        _listGameShape.add(new SignShape(_context, 0.8f*AppState.SIMULATION_HEIGHT, 1f, 0.07f*AppState.SIMULATION_HEIGHT, 0.6f, "Play", AppState.BALL));
        _listGameShape.add(new SignShape(_context, 0.6f*AppState.SIMULATION_HEIGHT, 1f, 0.07f*AppState.SIMULATION_HEIGHT, 0.6f, "Achievements", AppState.ACHIEVEMENT));
        _listGameShape.add(new SignShape(_context, 0.4f*AppState.SIMULATION_HEIGHT, 1f, 0.07f*AppState.SIMULATION_HEIGHT, 0.6f, "Exit", AppState.EXIT));
        _listGameShape.add(new StarShape(_context, 0.3f, 1f, 0.1f));
    }

    /**
     * Calculate the position of the objects in the list
     */
    public void calculateNextFrame(){

        for(int i=0; i<_listGameShape.size(); i++){
            if(_listGameShape.get(i).isMovable()){
                _listGameShape.get(i).moveOf(0f, 0f);
            }
            if(_listGameShape.get(i).isRotatable()){
                //_listGameShape.get(i).rotate();
                //Log.i(TAG, "Rotating object "+ _listGameShape.get(i).toString());
            }
        }
    }

    /**
     * handle the input touch event
     * @param e the motion event
     * @return true if the event have been handle
     */
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        // convert into the coordinates of the object of the screen
        if (e.getAction() == MotionEvent.ACTION_DOWN){
            Vec2 touch = new Vec2(e.getX(), AppState.DEVICE_HEIGHT - e.getY());
            touch = WorldCoord.getCoordinates(_bounce, touch);

            for (int i=0; i < _listGameShape.size(); i++){
                if(_listGameShape.get(i).isTouchable()){
                    if (_listGameShape.get(i).isTouched(touch)){
                        Log.i(TAG, _listGameShape.get(i).toString() + " is touched");
                        _bounce.changeState(_listGameShape.get(i).getUserData());
                        return true;
                    }
                }
            }
        }
        return false;
    }

}